package com.netease.xone.fragment;

import java.util.List;

import protocol.XoneCallBack;
import protocol.XoneService;
import protocol.XoneServiceCode;
import protocol.meta.UserDynamic;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.handmark.pulltorefresh.library.X1LoadingAdapterViewBase.OnLoadingListener;
import com.netease.pkgRelated.XoneConsts;
import com.netease.xone.R;
import com.netease.xone.adapter.DynamicAdapter;
import com.netease.xone.widget.listview.LoadingListView;


public class FragmentDynamicList extends FragmentBase {

    //Views
    private LoadingListView mLoadingList;
    private DynamicAdapter mAdapter;
    //Tid
    private int mDynamicTid;
    //Data
    private String[] mIds;


    public static FragmentDynamicList newInstance(){
        FragmentDynamicList f = new FragmentDynamicList();
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        XoneService.getInstance().addListener(mCallBack);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.view_loading_listview, null);
        mAdapter = new DynamicAdapter(getActivity());
        mLoadingList = (LoadingListView)view;
        mLoadingList.setAdapter(mAdapter);
        mLoadingList.disablePullToRefresh();
        mLoadingList.setOnLoadingListener(new OnLoadingListener() {

            @Override
            public void onRefreshing() {

            }

            @Override
            public void onLoading() {
                mDynamicTid = XoneService.getInstance().doGetDynamicIds(XoneConsts.LIST_ITEM_COUNT.MAX_COUNT, XoneConsts.LIST_ITEM_COUNT.ONE_REQUEST_COUNT);
            }

            @Override
            public void onLoadingMore() {
                int nowIndex = mAdapter.getCount();
                String[] ids = new String[XoneConsts.LIST_ITEM_COUNT.ONE_REQUEST_COUNT];
                for(int i = 0; i < ids.length; i++){
                    if(nowIndex + i < mIds.length){
                        ids[i] = mIds[nowIndex + i];
                    }else{
                        break;
                    }
                }
                mDynamicTid = XoneService.getInstance().doGetDynamicList(ids);
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mLoadingList.load();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        XoneService.getInstance().removeListener(mCallBack);
    }

    private XoneCallBack mCallBack = new XoneCallBack(){
        @Override
        public void onGetDynamicIds(int transactionId, Object[] dynamicListAndIds) {
            if(mDynamicTid == transactionId){
                List<UserDynamic> dynamicList = (List<UserDynamic>)dynamicListAndIds[0];
                mIds = (String[])dynamicListAndIds[1];
                if(dynamicList != null && dynamicList.size() != 0 && mIds != null){
                    mAdapter.addTail(dynamicList);
                    boolean hasMore = mAdapter.getCount() < mIds.length;
                    mAdapter.setHasMore(hasMore);
                    mLoadingList.onLoadingComplete(hasMore);
                }else{
                    mLoadingList.onNoContent(getString(R.string.success_dynamic_no_dynamic));
                }
            }
        };

        @Override
        public void onGetDynamicIdsError(int transactionId, int errCode, String err) {
            if(transactionId == mDynamicTid){
                if(errCode == XoneServiceCode.NETWORK_ERR_COMMON){
                    mLoadingList.onNoNetwork();
                }else{
                    mLoadingList.onLoadingError();
                }
            }
        };

        @Override
        public void onGetDynamicList(int transactionId, java.util.List<UserDynamic> dynamicList) {
            if(transactionId == mDynamicTid){
                if(dynamicList != null && dynamicList.size() != 0 && mIds != null){
                    mAdapter.addTail(dynamicList);
                    boolean hasMore = mAdapter.getCount() < mIds.length;
                    mAdapter.setHasMore(hasMore);
                    mLoadingList.onLoadingComplete(hasMore);
                }else{
                    mLoadingList.onNoContent(getString(R.string.success_dynamic_no_dynamic));
                }
            }
        };

        @Override
        public void onGetDynamicListError(int transactionId, int errCode, String err) {
            if(transactionId == mDynamicTid){
                mLoadingList.onLoadingComplete(false);
            }
        };
    };
}
